package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/29 19:07
 */
@RequestMapping(value = "/admin/order")
@Slf4j
@Api(tags = "管理端订单相关接口")
@RestController(value = "adminOrderController")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 管理端订单条件分页查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping(value = "/conditionSearch")
    public Result<PageResult> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("ordersPageQueryDTO = {}", ordersPageQueryDTO);
        PageResult pageResult = orderService.pageQueryOnAdmin(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 获取各个状态的订单数量
     *
     * @return
     */
    @GetMapping(value = "/statistics")
    @ApiOperation(value = "获取各个状态的订单数量")
    public Result<OrderStatisticsVO> getOrderStatistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.getOrderStatistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单详情
     *
     * @return
     */
    @ApiOperation(value = "查询订单详情")
    @GetMapping(value = "/details/{id}")
    public Result<OrderVO> getOrderDetail(@PathVariable(value = "id") Long id) {
        OrderVO orderVO = orderService.getOrderDetailByOrderId(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     *
     * @return
     */
    @ApiOperation(value = "接单")
    @PutMapping(value = "/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("ordersConfirmDTO = {}", ordersConfirmDTO);
        orderService.confirmOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @return
     */
    @PutMapping(value = "/rejection")
    @ApiOperation(value = "拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("ordersRejectionDTO = {}",ordersRejectionDTO);
        orderService.rejectOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     * @return
     */
    @PutMapping(value = "/cancel")
    @ApiOperation(value = "取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("ordersCancelDTO = {}",ordersCancelDTO);
        orderService.cancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @return
     */
    @PutMapping(value = "/delivery/{id}")
    @ApiOperation(value = "派送订单")
    public Result delivery(@PathVariable(value = "id") Long id){
        log.info("id = {}",id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @return
     */
    @ApiOperation(value = "完成订单")
    @PutMapping(value = "/complete/{id}")
    public Result complete(@PathVariable(value = "id") Long id){
        log.info("id = {}",id);
        orderService.complete(id);
        return Result.success();
    }
}
